package partitioner;

import Source.AccessSourceV2;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import transformation.Access;

/**
 * Create by zhh on 2022/9/6.
 */
public class PartitionAPP {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(3);
        DataStreamSource<Access> source = env.addSource(new AccessSourceV2());
        source.map(new MapFunction<Access, Tuple2<String, Access>>() {
            @Override
            public Tuple2<String, Access> map(Access access) throws Exception {
                return Tuple2.of(access.getDomain(), access);
            }
        }).partitionCustom(new ZHPartitioner(), new KeySelector<Tuple2<String, Access>, String>() {
            @Override
            public String getKey(Tuple2<String, Access> stringAccessTuple2) throws Exception {
                return stringAccessTuple2.f1.getDomain();
            }
        }).print();
//        .partitionCustom(new ZHPartitioner(),0)//选用自定义分区，用第一个位作为分区键
//                .map(new MapFunction<Tuple2<String, Access>, Access>() {
//                    @Override
//                    public Access map(Tuple2<String, Access> value) throws Exception {
//                        System.out.println("当前线程："+Thread.currentThread().getId()+",值为："+value);
//
//                        return value.f1;
//                    }
//                }).print();

        env.execute("partitionAPP");
    }
}
